<?php
namespace app\index\controller;

use app\index\logic\IndexLogic;
use app\index\service\IndexService;
use app\validate\FormVerify;
use think\captcha\Captcha;
use think\Controller;
use think\facade\Log;
use think\facade\Request;
use think\facade\Session;

class Index extends Controller
{
    //表单验证器
    private $formVerify;

    protected function initialize()
    {
        $this->formVerify = new FormVerify();
    }

    public function index()
    {
        //Log::write("111");
        return $this->fetch();

//        $index = new IndexLogic();
//        return $index::login();
    }

    public function login(Request $request)
    {
        //$index = new IndexLogic();
        $index = new IndexService();
        if($request::isPost()){
            //获取前台传输的参数
            $data = $request::param();
            //var_dump($this->formVerify);die;
            if(!$this->formVerify->check($data)){
                return ajaxReturn(false, $this->formVerify->getError());
            }
//            $username = $request::param('username');
//            $password = $request::param('password');
            $username =$data['username'];
            $password = $data['password'];

            //return $username. '|' .$password;
            return json($index->login($username, $password));
        }

        return $this->fetch();
    }

    //登录验证码方法
    public function verify()
    {
        $captcha = new Captcha();
        return $captcha->entry();
    }

    public function logout()
    {
        Session::clear();
        $this->redirect('/index/Index/login');
    }
}
